/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-6-11         luwei
 */

package com.tianci.appstore;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.SplashDialog;
import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.ThemeActivity;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.drawable.ScalingUtils;
import com.skyworth.webSDK.webservice.tcappstore.AppIndexAdBean;
import com.skyworth.webSDK.webservice.tcappstore.AppStoreException;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.controller.AppStoreBaseController;

import java.net.URI;

public abstract class SkyBaseActivity extends ThemeActivity {
    private static boolean bInit = false;
    private IAudio mAudio = null;
    private BroadcastReceiver mAudioReceiver = null;
    private IntentFilter mAudioFliter = null;


    public static class AppStoreSplashDialog extends SplashDialog {
        public AppStoreSplashDialog(Context context) {
            super(context, "appstore");
        }

        private CoocaaDraweeView createViewWithCache(SplashDialogData data) throws Exception {
            try {
                URI.create(data.url);
                CoocaaDraweeView view = new CoocaaDraweeView(mContext);
                view.setImageURI(Uri.parse(data.url));
                view.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
                return view;
            } catch (Exception e) {
                throw e;
            }
        }

        private CoocaaDraweeView createViewDefault() {
            CoocaaDraweeView view = new CoocaaDraweeView(mContext);
            if (!LiteAppApplication.isMarketChannel() && !LiteAppApplication.isCommonChannel()) {
                view.setImageURI(Android.getResourceURI(mContext, R.drawable.cover));
            }
            view.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE);
            LogUtils.d("create appstore splash with default");
            return view;
        }

        @Override
        protected View createContentView(SplashDialogData data) {
            setBackgroud(R.drawable.ui_sdk_app_bg);
            CoocaaDraweeView view = null;
//                    = new SimpleDraweeView(mContext);
//            view.getHierarchy().setPlaceholderImage(R.drawable.cover);
//            view.getHierarchy().set
//            if (data != null) {
//                view.setImageURI(Uri.parse(data.url));
//                view.getHierarchy().setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
//            }
            if (data != null) {
                try {
                    view = createViewWithCache(data);
                } catch (Exception e) {
                    view = createViewDefault();
                }
            } else
                view = createViewDefault();
            return view;
        }

        @Override
        protected SplashDialogData getOnlineData() throws Exception {
            try {
                AppIndexAdBean ad = CoocaaApplication.getCoocaaWebService().getAppStoreService().getAppIndexAd();
                SplashDialogData newCache = new SplashDialogData();
                newCache.duration = ad.getDuration();
                newCache.extra = ad.getExtra();
                try {
                    String url = ad.getUrl();
                    URI.create(url);
                    newCache.url = url;
                } catch (Exception e) {
                    throw e;
                }
                return newCache;
            } catch (AppStoreException e) {
                throw e;
            }
        }

        @Override
        protected void performDownloadData(final SplashDialogData data) {
//            final SimpleDraweeView v = new SimpleDraweeView(mContext);
//            DraweeController c = Fresco.newDraweeControllerBuilder().setControllerListener(new BaseControllerListener<ImageInfo>() {
//                @Override
//                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
//                    data.downloaded = true;
//                    saveCache(data);
//                }
//            }).setUri(Uri.parse(data.url)).build();
//            v.setController(c);
//            LiteHandlerThread.postOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    getContentView().addView(v, 0);
//                    v.setAlpha(0);
//                }
//            });
        }

        @Override
        public void dismiss() {
            super.dismiss();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initActivity(this);
    }

    private static synchronized void initActivity(Context context) {
        if (bInit)
            return;
        bInit = true;
        AppStoreBaseController.setContext(context);
//        SkyConstants.initResolution();
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAudioReceiver != null) {
            LogUtils.i("luwei", "initscene " + toString());
            registerReceiver(mAudioReceiver, mAudioFliter);
        }
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
    }

    @Override
    public void onPause() {
        super.onPause();
        try {
            if (mAudioReceiver != null) {
                unregisterReceiver(mAudioReceiver);
                LogUtils.i("luwei", "release " + toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mAudio = null;
        super.onDestroy();
    }

    public abstract String getName();

    public IAudio getAudioInterface() {
        return null;
    }

    public void initAudio() {
        mAudio = getAudioInterface();
        if (mAudio != null) {
            mAudioReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent arg1) {
                    LogUtils.d("luwei", "the action is " + arg1.getAction());
                    if ("com.tianci.appstore.QUERY".equals(arg1.getAction())) {
                        Intent intent = new Intent();
                        intent.setAction("com.tianci.appstore.COMMIT");
                        String json = mAudio.getCommand();
                        LogUtils.d("luwei", "the command is " + json);
                        intent.putExtra("SceneJson", mAudio.getCommand());
                        sendBroadcast(intent);
                    } else if ("com.tianci.appstore.EXECUTE".equals(arg1
                            .getAction())) {
                        mAudio.exeCommand(arg1);
                    }
                }
            };
            mAudioFliter = new IntentFilter();
            mAudioFliter.addAction("com.tianci.appstore.QUERY");
            mAudioFliter.addAction("com.tianci.appstore.EXECUTE");
            registerReceiver(mAudioReceiver, mAudioFliter);
            LogUtils.d("luwei", "init audio successful");
        }
    }

    private static final String APP_SESSION_KEY = "53bbb64256240b1b4c00b259";

    @Override
    protected final String getAppSessionKey() {
        return APP_SESSION_KEY;
    }
}
